﻿using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Services.WJK
{
    /// <summary>
    /// 员工部门业务
    /// </summary>
    public class Departments
    {
        /// <summary>
        /// 获取所有子部门ID
        /// </summary>
        /// <param name="list"></param>
        public static string GetChildDepartmentIds(List<Department> list, int parentId)
        {
            return GetChildList(list, parentId);
        }

        static string GetChildList(List<Department> deptList, int parentId)
        {
            var ids = new StringBuilder();
            Queue<int> queue = new Queue<int>();
            queue.Enqueue(parentId);
            while (queue.Count > 0)
            {
                //父节点
                var _parentId = queue.Dequeue();

                //子节点列表
                var childList = deptList.FindAll(x => x.Parentid == _parentId);
                if (childList.Count < 1)
                    continue;

                foreach (var child in childList)
                {
                    queue.Enqueue(child.Id);
                    ids.AppendFormat(",{0}", child.Id);
                }
            }

            return ids.Length > 0 ? ids.ToString().Substring(1) : string.Empty;
        }
    }
}
